package com.gf.message.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.gf.base.dao.impl.BaseDaoImpl;
import com.gf.base.type.MessageState;
import com.gf.base.type.MessageType;
import com.gf.common.bean.Page;
import com.gf.message.dao.MessageDao;
import com.gf.message.mapper.MessageMapper;
import com.gf.message.model.Message;

@Service("messageDao")
public class MessageDaoImpl extends BaseDaoImpl implements MessageDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	/**
	 * insert into message (senderId,senderName,receiveId,receiveName
	 * ,draftReceiveIds,draftReceiveNames
	 * ,title,content,createTime,messageType,senderState,receiverState) values
	 * (?,?,?,(select nickname from user where
	 * id=?),?,?,?,?,current_timestamp,?,?,?)
	 */
	@Value("${messageDao.insertSql}")
	private String insertSql;

	/**
	 * select * from message where senderId=? and messageType=?
	 */
	@Value("${messageDao.queryMessagesByMessageTypeSql}")
	private String queryMessagesByMessageTypeSql;

	/**
	 * select * from message where receiveId=? and messageType=?
	 */
	@Value("${messageDao.queryReceiveMessagesSql}")
	private String queryReceiveMessagesSql;

	/**
	 * update message set messageType=? where id=?
	 */
	@Value("${messageDao.updateMessageTypeSql}")
	private String updateMessageTypeSql;

	/**
	 * update message set senderState=? where id=?
	 */
	@Value("${messageDao.updateSenderStateSql}")
	private String updateSenderStateSql;
	
	/**
	 * update message set receiverState=? where id=?
	 */
	@Value("${messageDao.updateReceiverStateSql}")
	private String updateReceiverStateSql;

	/**
	 * select count(1) from message where receiveId=? and messageType=? and
	 * messageState=?
	 */
	@Value("${messageDao.queryNumberOfUnreadMessagesSql}")
	private String queryNumberOfUnreadMessagesSql;

	@Value("${messageDao.queryNumberOfReceiveMessagesSql}")
	private String queryNumberOfReceiveMessagesSql;

	@Value("${messageDao.queryNumberByMessageTypeSql}")
	private String queryNumberByMessageTypeSql;

	@Override
	public long insert(long senderId, String senderName, long receiveId,
			String draftReceiveIds, String draftReceiveNames, String title,
			String content, MessageType messageType, MessageState senderState,
			MessageState receiverState) {
		return this.insertWithIdReturn(insertSql, senderId, senderName,
				receiveId, receiveId, draftReceiveIds, draftReceiveNames,
				title, content, messageType.toString(), senderState.toString(),
				receiverState.toString());
	}

	@Override
	public int batchInsert(long senderId, String senderName, Long[] receiveIds,
			String draftReceiveIds, String draftReceiveNames, String title,
			String content, MessageType messageType, MessageState senderState,
			MessageState receiverState) {
		List<Object[]> argsList = new ArrayList<Object[]>();
		for (long receiveId : receiveIds) {
			Object[] obj = new Object[11];
			obj[0] = senderId;
			obj[1] = senderName;
			obj[2] = receiveId;
			obj[3] = receiveId;
			obj[4] = draftReceiveIds;
			obj[5] = draftReceiveNames;
			obj[6] = title;
			obj[7] = content;
			obj[8] = messageType.toString();
			obj[9] = senderState.toString();
			obj[10] = receiverState.toString();
			argsList.add(obj);
		}

		return this.batchInsertWithIdReturn(insertSql, argsList).size();
	}

	@Override
	public List<Message> queryMessagesByMessageType(long senderId,
			MessageType messageType, Page page) {
		return this.getPaginationResult(queryMessagesByMessageTypeSql, page,
				MessageMapper.getMapper(), senderId, messageType.toString(),MessageState.DELETED.toString());
	}

	@Override
	public List<Message> queryReceiveMessages(long receiveId, Page page) {
		return this.getPaginationResult(queryReceiveMessagesSql, page,
				MessageMapper.getMapper(), receiveId,
				MessageType.DRAFT.toString(),MessageState.DELETED.toString());
	}

	@Override
	public void updateMessageType(long id, MessageType messageType) {
		jdbcTemplate.update(updateMessageTypeSql, messageType.toString(), id);
	}

	@Override
	public void updateSenderState(long id, MessageState senderState) {
		jdbcTemplate.update(updateSenderStateSql, senderState.toString(), id);
	}
	
	@Override
	public void updateReceiverState(long id, MessageState receiverState) {
		jdbcTemplate.update(updateReceiverStateSql, receiverState.toString(), id);
	}

	@Override
	public int queryNumberOfUnreadMessages(long receiveId) {
		return jdbcTemplate.queryForInt(queryNumberOfUnreadMessagesSql,
				receiveId, MessageType.DRAFT.toString(),
				MessageState.UNREAD.toString());

	}

	@Override
	public int queryNumberOfReceiveMessages(long receiveId) {
		return jdbcTemplate.queryForInt(queryNumberOfReceiveMessagesSql,
				receiveId,MessageType.DRAFT.toString(),MessageState.DELETED.toString());
	}

	@Override
	public int queryNumberByMessageType(long senderId, MessageType messageType) {
		return jdbcTemplate.queryForInt(queryNumberByMessageTypeSql, senderId,
				messageType.toString(),MessageState.DELETED.toString());
	}


}
